import java.util.Collections;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 23735
 * Date: 2023-05-09
 * Time: 9:30
 */
public class Solution {
    public int findMinDifference(List<String> timePoints) {
        Collections.sort(timePoints);
        int result = 60 * 24;
        int len = timePoints.size();
        // 根据鸽巢原理, 每隔 60 * 24 分钟, 必有相同的时间点
        if (len > result){
            return 0;
        }
//         先排序, 排完序, 最小时间差要么在相邻两个, 要么在首尾两个
        Collections.sort(timePoints);
        int i = 0;
        int j = 1;
        // 判断相邻时间点
        while (j < len){
            int first = getMinute(timePoints.get(i));
            int second = getMinute(timePoints.get(j));
            result = Math.min(result, second - first);
            i++;
            j++;
        }
        // 判断首尾
        int first = getMinute(timePoints.get(0)) + 24 * 60;
        int tail = getMinute(timePoints.get(i));
        result = Math.min(result, first - tail);
        return result;
    }

    /**
     *  获取一个时间点的分钟数
     * @param time
     * @return
     */
    public int getMinute(String time){
        return ((time.charAt(0) - '0')*10 + time.charAt(1) - '0') * 60 +
                ((time.charAt(3) - '0')*10 + time.charAt(4) - '0');
    }
}
